﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Kinect;

using System.Windows.Controls;
using System.Windows;
using Biomedica.Graphics;
using NuiDicomPlayer.VirtualMouse;

using NuiDicomPlayer.DSP;
using DicomViewerAdvanced.VirtualMouse;

namespace NuiDicomPlayer.VirtualMouse
{
    

    public class MouseController
    {
        public Point ActivePointerPostion { set; get; }
     
        #region Normalisers
        const int buffersize = 10;
        VirtualMouse.Kinect.Filter _filterR = new Kinect.Filter();
        VirtualMouse.Kinect.Filter _filterL = new Kinect.Filter();
        DSP.DSP LH_kNormaliser_X = new DSP.DSP(buffersize);
        DSP.DSP RH_kNormaliser_X = new DSP.DSP(buffersize);
        DSP.DSP LH_kNormaliser_Y = new DSP.DSP(buffersize);
        DSP.DSP RH_kNormaliser_Y = new DSP.DSP(buffersize);
        #endregion


       public Vector2 HandsWorkingBox { set; get; }     
       public Vector2 ScreenWorkingBox { set; get; }
        Vector2 lastKnowPostionR;
        private float GlobalSmoth=2.0f;
       
        private Vector2 lastKnowPostionL;
        private Vector2 lastKnowPostion;

        public mouseImposter mouseleftpointer { set; get; }
        public mouseImposter mouserightpointer { set; get; }

        public MouseController(Canvas canvas, mouseImposter mouseleftpointer, mouseImposter mouserightpointer)
        {
            //this.canvas = canvas;
            //this.mouseleftpointer = mouseleftpointer;
            //this.mouserightpointer = mouserightpointer;

            //lastKnowPostion = Vector2.Zero;
            //lastKnowPostionR = Vector2.Zero;
            //lastKnowPostionL = Vector2.Zero;
            //HandsWorkingBox = new Vector2(4, 4);
           
            ScreenWorkingBox = new Vector2((float)canvas.ActualWidth, (float)canvas.ActualHeight);
        }
        public static Vector2 MapKinectSpace2Space(KinectSensor sensor,Vector2 vec,Vector2 ScreenDimention)
        {
             int width=0;
             int height=0;
             switch (sensor.DepthStream.Format)
                {
                    case DepthImageFormat.Resolution80x60Fps30:
                        width = 80;
                        height = 60;
                        break;
                    case DepthImageFormat.Resolution640x480Fps30:
                        width = 640;
                        height = 480;
                        break;
                    case DepthImageFormat.Resolution320x240Fps30:
                        width = 320;
                        height = 240;
                        break;

                }

             float nuValX = (width / ScreenDimention.X)*vec.X;
             float nuValY = (height / ScreenDimention.Y)*vec.Y;
             return new Vector2(nuValX, nuValY);

        }

        public Vector2 Convert(KinectSensor sensor, SkeletonPoint skeletonPoint)
        {

            int width = 0;
            int height = 0;
            int x = 0;
            int y = 0;

            if (sensor.DepthStream.IsEnabled)
            {
                var colorPoint = sensor.CoordinateMapper.MapSkeletonPointToDepthPoint(skeletonPoint, KinectSensor.KinectSensors[0].DepthStream.Format);
                x = colorPoint.X;
                y = colorPoint.Y;
               
                switch (sensor.DepthStream.Format)
                {
                    case DepthImageFormat.Resolution80x60Fps30:
                        width = 80;
                        height = 60;
                        break;
                    case DepthImageFormat.Resolution640x480Fps30:
                        width = 640;
                        height = 480;
                        break;
                    case DepthImageFormat.Resolution320x240Fps30:
                        width = 320;
                        height = 240;
                        break;

                }

            }

            return new Vector2((float)x / width, (float)y / height);
          

        }
       
      
      

    }
    [Flags]
    public enum MouseJoints
    {
        RightHand,
        LeftHand,
        Both
    }


}
